/// <reference name="MicrosoftAjax.js"/>

Type.registerNamespace('AjaxDataControls');

$ADC.GridViewDropDownListColumn = function() {
	this._dataSource;
	this._hasBinded = false;
	this._readOnly = false;
	this._dataField = '';
	this._dataTextField = '';
	this._dataTextFormatString = '';
	this._dataValueField = '';

	$ADC.GridViewDropDownListColumn.initializeBase(this);
	$ADC.GridViewDropDownListColumn.callBaseMethod(this, 'set_allowDragAndDrop', [false]);
}

$ADC.GridViewDropDownListColumn.prototype =
{
	initialize: function() {
		/// <summary>
		/// Initialize the column.
		/// </summary>
		$ADC.GridViewBaseColumn.callBaseMethod(this, 'initialize');
	},

	dispose: function() {
		/// <summary>
		/// Dispose the column.
		/// </summary>
		delete this._dataSource;
		delete this._hasBinded;
		delete this._readOnly;
		delete this._dataField;
		delete this._dataTextField;
		delete this._dataTextFormatString;
		delete this._dataValueField;

		$ADC.GridViewBaseColumn.callBaseMethod(this, 'dispose');
	},

	get_readOnly: function() {
		/// <value type="Boolean">
		/// Whether the value of the field can be modified in edit mode.
		/// </value>
		if (arguments.length !== 0) throw Error.parameterCount();
		return this._readOnly;
	},

	set_readOnly: function(value) {
		var e = Function._validateParams(arguments, [{ name: 'value', type: Boolean}]);
		if (e) throw e;

		if (this._readOnly != value) {
			this._readOnly = value;
			this.raisePropertyChanged('readOnly');
		}
	},

	get_dataField: function() {
		/// <value type="String">
		/// Use the <b>DataField</b> property to specify the name of the data field to bind to the GridViewDropDownListColumn object.
		/// </value>
		if (arguments.length !== 0) throw Error.parameterCount();
		return this._dataField;
	},

	set_dataField: function(value) {
		var e = Function._validateParams(arguments, [{ name: 'value', type: String}]);
		if (e) throw e;

		if (this._dataField != value) {
			this._dataField = value;
			this.raisePropertyChanged('dataField');
		}
	},

	get_dataTextField: function() {
		/// <value type="String">
		/// The <b>DataTextField</b> property is used to bind data into the dropdown list automatically, based on data of the DataSource property.
		/// If the <b>DataTextField</b> property is set to an empty string, no data is rendered.
		/// </value>
		if (arguments.length !== 0) throw Error.parameterCount();
		return this._dataTextField;
	},

	set_dataTextField: function(value) {
		var e = Function._validateParams(arguments, [{ name: 'value', type: String}]);
		if (e) throw e;

		if (this._dataTextField != value) {
			this._dataTextField = value;
			this.raisePropertyChanged('dataTextField');
		}
	},

	get_dataTextFormatString: function() {
		/// <value type="String">
		/// Use the <b>DataTextFormatString</b> property to specify a custom display format for the text displayed in the GridViewDropDownListColumn object. 
		/// If the DataFormatString property is not set, the field's value is displayed without any special formatting.
		/// </value>
		if (arguments.length !== 0) throw Error.parameterCount();
		return this._dataTextFormatString;
	},

	set_dataTextFormatString: function(value) {
		var e = Function._validateParams(arguments, [{ name: 'value', type: String}]);
		if (e) throw e;

		if (this._dataValueField != value) {
			this._dataTextFormatString = value;
			this.raisePropertyChanged('dataTextFormatString');
		}
	},

	get_dataValueField: function() {
		/// <value type="String">
		/// The <b>DataValueField</b> property is used to bind data into the dropdown list automatically, based on data of the DataSource property.
		/// If the <b>DataValueField</b> property is set to an empty string, no data is binded.
		/// </value>
		if (arguments.length !== 0) throw Error.parameterCount();
		return this._dataValueField;
	},

	set_dataValueField: function(value) {
		var e = Function._validateParams(arguments, [{ name: 'value', type: String}]);
		if (e) throw e;

		if (this._dataValueField != value) {
			this._dataValueField = value;
			this.raisePropertyChanged('dataValueField');
		}
	},

	get_dataSource: function() {
		/// <value type="Object">
		/// The <b>DataSource</b> property is the data used to bind the GridViewDropDownListColumn.
		/// If the <b>DataSource</b> property is set to null, no data is binded.
		/// </value>

		if (arguments.length !== 0) throw Error.parameterCount();
		return this._dataSource;
	},

	set_dataSource: function(value) {
		var e = Function._validateParams(arguments, [{ name: 'value', type: String, mayBeNull: true}]);
		if ((e == null) && (value != null))
			value = eval(value);

		e = Function._validateParams(arguments, [{ name: 'value', type: Array, mayBeNull: true}]);
		if (e) throw e;

		if (this._dataSource != value) {
			this._dataSource = value;
			this._hasBinded = false;
		}
	},

	_renderSelect: function(dataSource, select, selectedItem) {
		var e = Function._validateParams(arguments, [{ name: 'dataSource', type: Array }, { name: 'select', domElement: true }, { name: 'selectedItem', type: Object}]);
		if (e) throw e;

		var dataValueField = this.get_dataValueField();
		var dataTextField = this.get_dataTextField();
		var dataFormatString = this.get_dataTextFormatString();
		var firstItemText = ""; // future maybe. this.get_firstItemText(); this.set_firstItemText();

		var value = null;
		var text = '';

		if (this.get_readOnly()) {
			value = selectedItem.Value;
			text = selectedItem.Text;

			if (!$ADC.Util.isEmptyString(dataFormatString))
				text = text.localeFormat(dataFormatString);

			this._addItem(select, text, value);
			select.selectedIndex = 0;
		}
		else {
			this._addItem(select, firstItemText, "");
			var selectedValue = selectedItem.Value;

			for (var index = 0; index < dataSource.length; index++) {
				value = dataSource[index].Value;
				text = dataSource[index].Text;

				if (!$ADC.Util.isEmptyString(dataFormatString))
					text = text.localeFormat(dataFormatString);

				this._addItem(select, text, value, dataSource[index]);
				if (value == selectedValue)
					select.selectedIndex = (index + 1);

				value = null;
				text = "";
			}

			if (selectedValue == "none")
				select.selectedIndex = 0;
		}
	},

	_addItem: function(select, text, value, item) {
		var e = Function._validateParams(arguments, [{ name: 'select', domElement: true }, { name: 'text', type: String }, { name: 'value', type: String }, { name: 'item', type: Object, mayBeNull: true, optional: true}]);
		if (e) throw e;

		if (typeof item == 'undefined') item = null;

		var opt = document.createElement('option');
		opt.get_item = function() { return item; }
		select.appendChild(opt);

		if (typeof opt.innerText != 'undefined')
			opt.innerText = text;
		else if (typeof opt.textContent != 'undefined')
			opt.textContent = text;

		if (value != null)
			opt.value = value;
	},

	renderData: function(dataRow, row, container) {
		/// <summary>
		/// This method is used to render the data cell. You do not have to call this method in your code.
		/// </summary>
		/// <param name="dataRow" type="Object">
		/// The item of the GridView dataSource.
		/// </param>
		/// <param name="row" type="AjaxDataControls.GridViewRow">
		/// The GridViewRow which is passed to render the column data cell.
		/// </param>
		/// <param name="container" domElement="true">
		/// The container cell(td) to render the data of this column.
		/// </param>

		var e = Function._validateParams(arguments, [{ name: 'dataRow', type: Object }, { name: 'row', type: $ADC.GridViewRow }, { name: 'container', type: Object}]);
		if (e) throw e;

		var dataField = this.get_dataField();
		if ($ADC.Util.isEmptyString(dataField))
			throw Error.invalidOperation('DataField set before calling this method.');

		var dataValueField = this.get_dataValueField();
		if ($ADC.Util.isEmptyString(dataValueField))
			throw Error.invalidOperation('DataValueField set before calling this method.');

		var dataTextField = this.get_dataTextField();
		if ($ADC.Util.isEmptyString(dataTextField))
			throw Error.invalidOperation('DataTextField set before calling this method.');

		var dataSource = this.get_dataSource();
		var e = Function._validateParameter(dataSource, Array, false, false);
		if (e) throw Error.invalidOperation('DataSource set before calling this method.');

		var value = null;
		if ((dataField != null) && (dataField.length > 0))
			value = dataRow[dataField];

		if (value == null) {
			value = dataSource[0];
		}
		else {
			for (var index = 0; index < dataSource.length; index++) {
				if (dataSource[index].Value == value) {
					value = dataSource[index];
					break;
				}
			}
		}

		if ((row.get_rowType() == $ADC.GridViewRowType.EditRow)) {
			var select = document.createElement('select');
			select.disabled = this.get_readOnly();

			this._renderSelect(dataSource, select, value);
			select.get_selectedItem = function() { return this.options[this.selectedIndex].get_item(); };
			select.get_selectedValue = function() { return this.options[this.selectedIndex].get_item().Value; };
			select.get_selectedText = function() { return this.options[this.selectedIndex].get_item().Text; };

			container.appendChild(select);
			if (this.get_controlStyle() != null)
				this.get_controlStyle().apply(select);
		}
		else {
			value = (value != null) ? value.Text : "Unbound";
			container.appendChild(document.createTextNode(value));
		}

		if (this.get_itemStyle() != null)
			this.get_itemStyle().apply(container);

		this._hasBinded = true;
	}
}

$ADC.GridViewDropDownListColumn.registerClass('AjaxDataControls.GridViewDropDownListColumn', $ADC.GridViewBaseColumn);

if (typeof (Sys) != 'undefined') Sys.Application.notifyScriptLoaded();